package com.xuqi.FileBrowse;

import java.io.File;
import java.util.ArrayList;

import com.xuqi.R;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

public class FileBrowseControl extends ViewGroup
{

	private Button m_btBrowse = null;
	private EditText m_etPath = null;
	private Context m_cContext = null;
	private LayoutInflater m_layoutInflater = null;
	private View m_cView = null;
	private Button btBackButton = null;
	private EditText etCurrentText = null;
	private String m_strCurrentPath = null;
	private ListView m_lsListView = null;
	private ArrayList<String> m_strFileList = null;
	public FileBrowseControl(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		m_cContext = context;
		m_layoutInflater = LayoutInflater.from(context);
		if (m_layoutInflater != null)
		{
			m_cView = m_layoutInflater.inflate(R.layout.filebrowsecontrol, null);
		}
		addView(m_cView);

		m_etPath = (EditText) findViewById(R.id.edPath);
		m_etPath.setEllipsize(TruncateAt.START);
		m_btBrowse = (Button) findViewById(R.id.btSearch);
		LinearLayout.LayoutParams lpPath = (LinearLayout.LayoutParams) m_etPath.getLayoutParams();
		LinearLayout.LayoutParams lpBrowse = (LinearLayout.LayoutParams) m_btBrowse.getLayoutParams();
		int screenWidth = this.getResources().getDisplayMetrics().widthPixels;
		lpBrowse.width = 100;
		lpPath.width = screenWidth - 100;
		m_etPath.setLayoutParams(lpPath);
		m_btBrowse.setLayoutParams(lpBrowse);
		
		m_btBrowse.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				// TODO Auto-generated method stub
				final Dialog dialog = new Dialog(m_cContext, R.style.MyDialog);
				dialog.setContentView(R.layout.filebrowsedialog);
				etCurrentText = (EditText) dialog.findViewById(R.id.etCurrentPath);
				btBackButton = (Button) dialog.findViewById(R.id.btBack);
				etCurrentText.setText("/sdcard/");
				etCurrentText.setSingleLine();
				etCurrentText.setEllipsize(TruncateAt.START);
				
				LinearLayout.LayoutParams lpCurrentPath = (LinearLayout.LayoutParams) etCurrentText.getLayoutParams();
				LinearLayout.LayoutParams lpBack= (LinearLayout.LayoutParams) btBackButton.getLayoutParams();
				int screenWidth = getResources().getDisplayMetrics().widthPixels;
				lpBack.width = 100;
				lpCurrentPath.width = screenWidth - 150;
				etCurrentText.setLayoutParams(lpCurrentPath);
				btBackButton.setLayoutParams(lpBack);
				
				m_strCurrentPath = etCurrentText.getText().toString();
				m_lsListView = (ListView) dialog.findViewById(R.id.lsFile);
				OnItemClickListener itemListener = new OnItemClickListener()
				{
					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
					{
						m_strCurrentPath += m_strFileList.get(arg2);
						File file = new File(m_strCurrentPath);
						if (file.exists())
						{
							if (file.isFile())
							{
								dialog.dismiss();
								m_etPath.setText(m_strCurrentPath);
							}
							else
							{
								m_strCurrentPath += File.separatorChar;
								updateList();
								
							}
							etCurrentText.setText(m_strCurrentPath);
							
						}
					}
				};
				m_lsListView.setOnItemClickListener(itemListener);
				
				btBackButton.setOnClickListener(new View.OnClickListener()
				{
					@Override
					public void onClick(View v)
					{

						if (m_strCurrentPath.length() > 1)
						{
							m_strCurrentPath = m_strCurrentPath.substring(0,m_strCurrentPath.lastIndexOf(File.separatorChar));
							m_strCurrentPath = m_strCurrentPath.substring(0,m_strCurrentPath.lastIndexOf(File.separatorChar)+1);
							etCurrentText.setText(m_strCurrentPath);
							updateList();
						}
					}
				});
				updateList();
				dialog.show();
				
				Window window = dialog.getWindow();
				
				if (window != null) {
					WindowManager.LayoutParams lp = window.getAttributes();
					if (lp != null) {
						lp.width = LayoutParams.FILL_PARENT;
						lp.height = LayoutParams.FILL_PARENT;
						window.setAttributes(lp);
					}
				}
			}
		});

	}
	
	public FileBrowseControl(Context context, AttributeSet attrs)
	{
		this(context, attrs, 0);

	}

	private void updateList()
	{
	
		File file = new File(m_strCurrentPath);
		m_strFileList = new ArrayList<String>();
		m_strFileList.clear();
		if(file.exists())
		{
			File[] files = file.listFiles();
			for(int i = 0; i < files.length; i++)
			{
				m_strFileList.add(files[i].getName());
			}
		}
		m_lsListView.setAdapter(new ArrayAdapter<String>(m_cContext, android.R.layout.simple_expandable_list_item_1, m_strFileList));
	}

	public FileBrowseControl(Context context)
	{
		this(context, null, 0);
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b)
	{
		// TODO Auto-generated method stub
		int screenHeight = this.getResources().getDisplayMetrics().heightPixels;
		int screenWidth = this.getResources().getDisplayMetrics().widthPixels;
		m_cView.measure(screenWidth, screenHeight);
		m_cView.layout(0, 0, screenWidth, screenHeight);
	}

}
